/**
 * 
 */
package org.whuims.bestmatch.tools;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

/**
 * @author ChengQikai
 * @version: 1.0
 */
public class ThreeFolder {
	String model = "LM_DIR";
	int stemMode = 1;

	public ThreeFolder(String model, int stemMode) {
		super();
		this.model = model;
		this.stemMode = stemMode;
	}

	public void analyze() throws IOException, InterruptedException {
		if (model.equals("LM_DIR")) {
			analyzeLM();
		} else if (model.equals("BM25")) {
			analyzeBM();
		}
	}

	/**
	 * 
	 */
	private void analyzeBM() {

	}

	public void analyzeLM() throws IOException, InterruptedException {
		this.foldTrainLM(1, 2, 3);
		this.foldTrainLM(1, 3, 2);
		this.foldTrainLM(2, 3, 1);
	}

	/**
	 * @param testFold
	 * @param mu
	 * @throws IOException
	 * @throws InterruptedException
	 */
	private String foldTrainLM(int fold1, int fold2, int testFold) throws IOException, InterruptedException {
		for (int mu = 1000; mu < 3000; mu += 100) {
			int i = mu;
			String rule = "<rule>method:dir,mu:" + mu + "</rule>";
			QueryIniFactory factory = new QueryIniFactory(rule, this.stemMode, 1000);
			System.out.println("--------------------------------");
			String queryString = factory.makeBunchQuerys(fold1, fold2);
			try {
				File parameterFile = new File(Config.getProp("indri_dir_path") + "train\\" + mu + ".ini");
				FileUtils.write(parameterFile, queryString);
				String outPutFilePath = Config.getProp("indri_dir_path") + "temp\\" + parameterFile.getName()
						+ "result";
				File outPutFile = new File(outPutFilePath);
				if (!outPutFile.getParentFile().exists()) {
					outPutFile.getParentFile().mkdirs();
				}
				this.runIndriQuery(parameterFile, outPutFile);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		String mu = "";
		double ndcgMax = 0;
		File dir = new File(Config.getProp("indri_dir_path") + "temp");
		for (File file : dir.listFiles()) {
			SingleFileEval eval = new SingleFileEval("resource/qrels_format.test", file.getAbsolutePath());
			try {
				double ndcg = eval.evalNDCG();
				if (ndcg > ndcgMax) {
					ndcgMax = ndcg;
					mu = StringUtils.substringBefore(file.getName(), ".");
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		String rule = "<rule>method:dir,mu:" + mu + "</rule>";
		System.out.println("rule:\t" + rule);
		QueryIniFactory factory = new QueryIniFactory(rule, this.stemMode, 1000);
		String query = factory.makeQuerys(testFold);
		File testFile = new File(Config.getProp("indri_dir_path") + "train\\test.ini");
		try {
			FileUtils.write(testFile, query);
		} catch (IOException e) {
			e.printStackTrace();
		}
		File testResult = new File(Config.getProp("indri_dir_path") + "temp\\test.iniresult");
		this.runIndriQuery(testFile, testResult);
		SingleFileEval eval = new SingleFileEval("resource/qrels_format.test", testResult.getAbsolutePath());
		try {
			FileUtils
					.write(new File(Config.getProp("indri_dir_path") + "result\\" + testFold + ".result"), eval.eval());
			FileUtils.deleteDirectory(testFile.getParentFile());
			FileUtils.deleteDirectory(testFile.getParentFile());
		} catch (IOException e2) {
			e2.printStackTrace();
		}

		return "1000";
	}

	private boolean runIndriQuery(File parameterFile, File outPutFile) throws IOException {
		String command = "cmd /C " + Config.getProp("indri_path") + " " + parameterFile.getAbsolutePath() + " >"
				+ outPutFile.getAbsolutePath();
		Process process = Runtime.getRuntime().exec("cmd /C" + command);
		try {
			process.waitFor();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		String temp = FileUtils.readFileToString(outPutFile);
		temp = temp.replaceAll("/media/Bin/RCV1/\\d+/", "");
		temp = temp.replace("newsML.xml", "");
		FileUtils.write(outPutFile, temp);
		return true;
	}

	public static void main(String[] args) {
		ThreeFolder thf = new ThreeFolder("LM_DIR", QueryIniFactory.STEM_KROVETZ);
		try {
			thf.analyze();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
